package com.yilin.tms.order.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.commons.util.DateTimeUtil;
import com.yilin.commons.util.MathUtil;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.order.transit.TransitOrdinaryOrder;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.order.mapper.transit.TransitOrdinaryOrderMapper;
import com.yilin.tms.order.service.order.ITransitOrdinaryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class TransitOrdinaryService extends ServiceImpl<TransitOrdinaryOrderMapper, TransitOrdinaryOrder> implements ITransitOrdinaryService {
    @Resource
    private TransitOrdinaryOrderMapper transitOrdinaryOrderMapper;

    @Override
    public void saveOrUpdateTransitOrdinary(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        Double totalCost = MathUtil.multiply(transitOrdinaryOrder.getFreightUnitPrice(), transitOrdinaryOrder.getUnloadingWeight(), 2);
        double proudCoust = MathUtil.subtract(transitOrdinaryOrder.getLoadingWeight(), transitOrdinaryOrder.getUnloadingWeight());
        transitOrdinaryOrder.setFreightCost(totalCost);
        transitOrdinaryOrder.setPoundDifference(proudCoust);
        transitOrdinaryOrder.setOrganizeId(account.getOrganizeId());
        transitOrdinaryOrder.setOrganizeName(account.getOrganizeName());
        transitOrdinaryOrder.setSuperiorPlatId(account.getSuperiorPlatId());
        transitOrdinaryOrder.setSuperiorPlatName(account.getSuperiorPlatName());
        transitOrdinaryOrder.setAccountId(account.getId());
        transitOrdinaryOrder.setAccountName(account.getName());
        transitOrdinaryOrder.setTransitStatus(TransitOrdinaryOrder.TransitStatus.auditing);
       if(StringUtil.isEmpty(transitOrdinaryOrder.getTransitNo())) transitOrdinaryOrder.setTransitNo(DateTimeUtil.dateNo("PT"));
        transitOrdinaryOrder.setAuditingId(null);
        transitOrdinaryOrder.setAuditingName(null);
        transitOrdinaryOrder.setAuditingDate(null);
        transitOrdinaryOrder.setPaymentId(null);
        transitOrdinaryOrder.setPaymentName(null);
        transitOrdinaryOrder.setPaymentDate(null);
        transitOrdinaryOrder.setReturnRemark(null);
        saveOrUpdate(transitOrdinaryOrder);
    }

    //获取分页
    @Override
    public PageData<TransitOrdinaryOrder> getTransitOrdinaryPage(Account loginUser, TransitOrdinaryOrder query, Integer start, Integer limit) {
        QueryWrapper<TransitOrdinaryOrder> queryWrapper = new QueryWrapper<>();
        if (loginUser.getUserType() != UserType.platformUser && loginUser.getUserType() != UserType.adminUser) {
            queryWrapper.and(wrapper ->
                    wrapper.eq("organize_id", loginUser.getOrganizeId())
                            .or()
                            .eq("superior_plat_id", loginUser.getOrganizeId())
            );
        }
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return transitOrdinaryOrderMapper.selectPage(new PageData<>(start, limit), queryWrapper);
    }

    //根据ID获取
    @Override
    public TransitOrdinaryOrder getTransitOrdinaryOrderById(String id) {
        return transitOrdinaryOrderMapper.selectById(id);
    }

    //审核
    @Override
    public void auditingTransitOrdinaryOrder(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        transitOrdinaryOrder.setAuditingId(account.getId());
        transitOrdinaryOrder.setAuditingName(account.getName());
        if (transitOrdinaryOrder.getAuditingDate() == null) transitOrdinaryOrder.setAuditingDate(new Date());
        saveOrUpdate(transitOrdinaryOrder);
    }

    //付款完成
    @Override
    public void paymentTransitOrdinaryOrder(Account account, TransitOrdinaryOrder transitOrdinaryOrder) {
        transitOrdinaryOrder.setPaymentId(account.getId());
        transitOrdinaryOrder.setPaymentName(account.getName());
        transitOrdinaryOrder.setTransitStatus(TransitOrdinaryOrder.TransitStatus.success);
        if (transitOrdinaryOrder.getPaymentDate() == null) transitOrdinaryOrder.setPaymentDate(new Date());
        saveOrUpdate(transitOrdinaryOrder);
    }

    //删除
    @Override
    public void deleteTransitOrdinaryOrder(String id) {
        transitOrdinaryOrderMapper.deleteById(id);
    }
}
